package com.qf.ability.search.listener;

import com.ken.event.action.apply.consumer.IKenEventHandler;
import com.ken.event.action.apply.consumer.KenEvent;
import com.ken.event.standard.entity.KenMessage;
import com.qf.ability.search.entity.SearchCourseEntity;
import com.qf.ability.search.entity.SearchGuigeEntity;
import com.qf.ability.search.service.ICourseIndexSearchService;
import com.qf.commons.core.utils.QfBeanUtils;
import com.qf.data.course.dto.CourseSearchDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 课程新增的事件处理器
 * author Ken
 * create_time 2022/9/26
 */
@KenEvent("course_insert")
@Slf4j
public class CouseInsertEventHandler implements IKenEventHandler<CourseSearchDto> {

    @Autowired
    private ICourseIndexSearchService service;

    @Override
    public void eventHandler(CourseSearchDto courseSearchDto, KenMessage kenMessage) {
        log.debug("[event handler] 接收到课程新增的消息 - {}", courseSearchDto);
        //CourseSearchDto -> SearchCourseEntity
        SearchCourseEntity searchCourse = QfBeanUtils.copyBean(courseSearchDto, SearchCourseEntity.class);
        //额外处理下规格的List集合的转换 BeanUtils.copy方法不能拷贝集合
//        List<SearchGuigeEntity> searchGuigeEntities = courseSearchDto.getGuiges().stream().map(guiges ->
//                        new SearchGuigeEntity().setGid(guiges.getGid()).setValid(guiges.getValid()))
//                .collect(Collectors.toList());

        List<SearchGuigeEntity> searchGuigeEntities = QfBeanUtils.copyList(courseSearchDto.getGuiges(), SearchGuigeEntity.class);
        searchCourse.setGuiges(searchGuigeEntities);

        log.debug("[event handler] 课程实体转换 - {}", searchCourse);
        //直接调用业务层保存课程信息即可
        service.saveCourse(searchCourse);
    }
}
